#include <iostream>
#include <stdlib.h>
#include <unistd.h>
#include <sstream>

#include <Logger.h>
#include <Caja.h>
#include <BaseException.h>

using namespace std;

int main(int argc, char* argv[]) 
{
	Logger* log = NULL;
	Caja* caja = NULL;
	
	try
	{
		log = Logger::getInstance();
		caja = new Caja();
		
		stringstream ss;
		ss << "CAJA >> Proceso caja iniciado >> PID: " << getpid();
		log->info(ss.str());
		
		while(caja->hayPedidosDeCaja())
		{
			caja->procesarPedidoDeCaja();
		}
	}
	catch(BaseException& e)
	{
		if(log != NULL)
		{
			log->error(e.what());
		}
		std:cerr << "CAJA:" << e.what() << std::endl;
	}
	
	if(caja != NULL)
	{
		delete caja;
		caja = NULL;
	}
	
	if(log != NULL)
	{
		log->info("CAJA >> Finalizando");
	}
	
	// Libero recursos
	Logger::destroy();
}